<?php
/**
 * poste/index.phtml
 */
?>
<?php
$title	 = 'Postes';
$this->headTitle($title);
?>
<div class="content-head">
    <h3><?php echo $title; ?></h3>
    <div class="toolbar">
	<div class="toggle add" title="Ajouter">
            <a href="#" class='addTitle' style="display: block; width: 100%; height: 100%;"></a>
        </div>
    </div>
</div>

<div class="content-body">

    <div class="add-toggle">
	<table class="table table-striped list-table">
	    <thead>
		<tr>
		    <td width="25%">Code</td>
		    <td width="35%">Libellé</td>
		    <td width="10%">Niveau</td>
		    <td width="10%">Echelon</td>
		    <td width="10%">Collège</td>
		    <td width="10%">&nbsp;</td>
		</tr>
	    </thead>
	    <tbody>
		<tr>
		    
		    <td>
			<a href="#" id="newCode" data-type="text"></a>
		    </td>
		    <td>
			<a href="#" id="newLibelle" data-type="text"></a>
		    </td>
		    <td>
			<a href="#" id="newNiveau" data-type="select"></a>
		    </td>
		    <td>
			<a href="#" id="newEchelon" data-type="select"></a>
		    </td>
                    <td>
			<a href="#" id="newCollege" data-type="select"></a>
		    </td>
		    <td>
			<button id="newAdd" class="btn btn-success">Ajouter</button>
		    </td>
		</tr>
	    </tbody>
	</table>
	<form id="newForm" action="#" method="post">
	    <input type="hidden" id="data-code" name="code" value="">
	    <input type="hidden" id="data-libelle" name="libelle" value="">
	    <input type="hidden" id="data-echelon" name="echelon" value="1">
	    <input type="hidden" id="data-niveau" name="niveau" value="1">
	    <input type="hidden" id="data-college" name="college" value="0">
	</form>
    </div>

    <!-- AJAX CONTENT -->
    <div id="content-data">
	<!-- AJAX LOADING -->
    </div>
</div>

<script type="text/javascript">
    // Démarrage
    $(function() {
	// Ajouter
	$('.addTitle').click(function(e) {
	    e.preventDefault();
	    $('.add-toggle').toggle('normal');
	});

	// Nouveau Code
	$('#newCode').editable({
	    title: 'Nouveau Code',
	    validate: function(value) {
		$('#data-code').val(value);
		affAddButton(verifyData(false));
	    }
	});

	// Nouveau libellé
	$('#newLibelle').editable({
	    title: "Nouveau Libellé",
	    validate: function(value) {
		$('#data-libelle').val(value);
		affAddButton(verifyData(false));
	    }
	});

	// Nouvel Echelon
<?php
$echelon = array();
for ($i = 1; $i < 11; $i++) {
    $echelon[$i] = 'Echelon ' . \RHX\Helper\Number::getRomanNumber($i);
}
$echelonSource	 = json_encode($echelon);
?>
	$('#newEchelon').editable({
	    title: "Echelon",
	    source: '<?php echo $echelonSource; ?>',
	    validate: function(value) {
		$('#data-echelon').val(value);
		affAddButton(verifyData(false));
	    }
	});

	// Nouveau Niveau
<?php
$niveau		 = array();
for ($i = 1; $i < 11; $i++) {
    $niveau[$i] = "Niveau $i" /* . \RHX\Helper\Number::getRomanNumber($i) */;
}
$niveauSource	 = json_encode($niveau);
?>
	$('#newNiveau').editable({
	    title: 'Niveau',
	    source: '<?php echo $niveauSource; ?>',
	    validate: function(value) {
		$('#data-niveau').val(value);
		affAddButton(verifyData(false));
	    }
	});

	// Nouveau Collège
<?php
$college	 = array();
for ($i = 1; $i < 11; $i++) {
    $college[$i] = 'College ' . \RHX\Helper\Number::getRomanNumber($i);
}
$collegeSource = json_encode($college);
?>
	$('#newCollege').editable({
	    title: 'College',
	    source: '<?php echo $collegeSource; ?>',
	    validate: function(value) {
		$('#data-college').val(value);
		affAddButton(verifyData(false));
	    },
	    placement: 'left'
	});

	// Ajouter nouveau
	$('#newAdd').click(function(e) {
	    e.preventDefault();
	    if (!verifyData(true))
		return false;
	    ajax = $.ajax({
		dataType: 'json',
		type: 'post',
		url: '<?php echo $this->url('RHX/default', array('controller'	 => 'poste', 'action'	 => 'add')) ?>',
		data: $('#newForm').serialize()
	    });
	    ajax.done(function(result) {
		if (!result.done) {
		    alert("Une erreur est survenue lors de l'insertion des données\n" + result.error);
		}
		window.location.reload();
	    });
	});

	// Masquage du bouton
	affAddButton(false);

	// Affichage du contenu
	refresh();
    });

    /**
     * rafraichissement

     * @returns {undefined}     */
    function refresh() {
	ajax = $.ajax({
	    type: 'post',
	    url: '<?php echo $this->url('RHX/default', array('controller'	 => 'poste', 'action'	 => 'loadIndex')) ?>'
	});
	ajax.done(function(data) {
	    $('#content-data').hide().html(data).fadeIn('normal');
	});
    }

    /**
     * Vérification des données
     *
     * @param {Boolean} msg
     * @returns {Boolean}
     */
    function verifyData(msg) {
	libelle = $('#data-libelle').val();
	libelle = $.trim(libelle);

	if (libelle === '') {
	    if (msg)
		alert("Le libellé ne peut pas être vide");

	    return false;
	}

	if (libelle.length < 3) {
	    if (msg)
		alert("Le libellé est trop court");

	    return false;
	}

	return true;
    }

    /**
     * Affichage / Masquage du bouton d'ajout
     *
     * @param {Boolean} show
     * @returns {undefined}
     */
    function affAddButton(show) {
	if (show) {
	    $('#newAdd').show('normal');
	} else {
	    $('#newAdd').hide('normal');
	}
    }
</script>